package cn.findviewbydream.evil.main.service.impl;

import cn.findviewbydream.evil.main.dao.BaseDAO;
import cn.findviewbydream.evil.main.dao.SettingEmployeeDAO;
import cn.findviewbydream.evil.main.entity.BaseTypeOfWorkEntity;
import cn.findviewbydream.evil.main.entity.EmployeeEntity;
import cn.findviewbydream.evil.main.service.SettingEmployeeService;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/********************************************
 * 工程名：MealOrder
 * 包名：cn.findviewbydream.evil.main.service.impl
 * 时间：17-3-2
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：
 ********************************************/
@Service
public class SettingEmployeeServiceImpl implements SettingEmployeeService
{
    private final SettingEmployeeDAO dao;
    private final BaseDAO baseDAO;

    @Autowired
    public SettingEmployeeServiceImpl(SettingEmployeeDAO settingEmployeeDAO, BaseDAO baseDAO)
    {
        this.dao = settingEmployeeDAO;
        this.baseDAO = baseDAO;
    }


    @Override
    public List<EmployeeEntity> getEmployeeList(boolean isAll)
    {
        return dao.selectEmployeeList(isAll);
    }

    @Override
    public EmployeeEntity getEmployee(String id)
    {
        return dao.selectEmployee(id);
    }

    @Override
    public List<BaseTypeOfWorkEntity> getTypeOfWorkList()
    {
        return dao.selectTypeOfWorkList();
    }

    @Override
    public String postNewEmployee(String employeeId, String name, int typeOfWorkId, int salary, Integer bonus,
                                  Date entryDateTime, Date leaveDateTime)
    {
        EmployeeEntity entity = new EmployeeEntity();

        if (employeeId == null)
        {
            BaseTypeOfWorkEntity typeOfWorkEntity = baseDAO.selectTypeOfWorkById(typeOfWorkId);
            char[] cs = typeOfWorkEntity.getType().toCharArray();
            /*获取type首字母拼音*/
            char typeFirstPinYin = PinyinHelper.toHanyuPinyinStringArray(cs[0])[0]
                    .toUpperCase().toCharArray()[0];
            SimpleDateFormat format = new SimpleDateFormat("yyMMdd");
            /*ID前缀，由工种类型首字母(大写)+日期构成*/
            String prefix = typeFirstPinYin + format.format(new Date());
            /*通过前缀like查询出最后一个相似的Id*/
            String lastId = dao.selectLastEmployeeId(prefix + "%");
            /*判断是否为空且再次进行判断Id是否包含，否则新成立Id*/
            if (lastId != null && lastId.contains(prefix))
            {
                int num = 0;
                try
                {
                    num = Integer.valueOf(lastId.replace(prefix, ""));
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
                num = num + 1;
                if (num < 9)
                    employeeId = prefix + "0" + num;
                else if (num < 99)
                    employeeId = prefix + num;
                else
                    return null;
            }
            else
            {
                employeeId = prefix + "01";
            }
        }
        entity.setEmployeeId(employeeId);
        entity.setName(name);
        entity.setTypeOfWork(new BaseTypeOfWorkEntity(typeOfWorkId));
        entity.setSalary(salary);
        entity.setBonus(bonus);
        entity.setEntryDateTime(new Date());
        entity.setLeaveDateTime(null);

        return dao.insertEmployee(entity) == 1 ? entity.getEmployeeId() : null;
    }

    @Override
    public boolean postEmployee(String employeeId, String name, int typeOfWorkId, int salary, Integer bonus,
                                Date entryDateTime, Date leaveDateTime)
    {
        EmployeeEntity entity = dao.selectEmployee(employeeId);
        entity.setEmployeeId(employeeId);
        entity.setName(name);
        entity.setTypeOfWork(new BaseTypeOfWorkEntity(typeOfWorkId));
        entity.setSalary(salary);
        entity.setBonus(bonus);
        entity.setEntryDateTime(entryDateTime);
        entity.setLeaveDateTime(leaveDateTime);

        return dao.updateEmployee(entity) == 1;
    }

    @Override
    public String postEmployeeLeave(String employeeId)
    {
        EmployeeEntity entity = dao.selectEmployee(employeeId);
        System.out.println(entity.toString());
        entity.setLeaveDateTime(new Date());
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");

        if (dao.updateEmployee(entity) == 1)
            return format.format(entity.getLeaveDateTime());
        else
            return null;
    }
}
